package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.EquipmentApply;
import com.hushao.lims.weblims.domain.EquipmentInfo;
import com.hushao.lims.weblims.domain.EquipmentRepertory;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.EquipmentApplyRepository;
import com.hushao.lims.weblims.service.EquipmentApplyService;
import com.hushao.lims.weblims.service.EquipmentRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/25
 */
@Service
public class EquipmentApplyServiceImpl implements EquipmentApplyService {
    @Autowired
    private EquipmentApplyRepository equipmentApplyRepository;

    @Autowired
    private EquipmentRepertoryService equipmentRepertoryService;
    @Override
    public List<EquipmentApply> listEquipmentAndCountAndState(EquipmentInfo equipmentInfo, Integer equipmentCount, String s) {
        return equipmentApplyRepository.findByEquipmentInfoAndEquipmentApplyCountAndEquipmentApplyState(equipmentInfo,equipmentCount,s);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentApply update(EquipmentApply ea) {
        return equipmentApplyRepository.save(ea);
    }

    @Override
    public List<EquipmentApply> listEquipmentAndGiveBackAndState(EquipmentInfo equipmentInfo, TeacherInfo teacherInfo, String s) {
        return equipmentApplyRepository.findByEquipmentInfoAndTeacherInfoAndEquipmentApplyState(equipmentInfo,teacherInfo,s);
    }

    @Override
    public void update(List<EquipmentApply> equipmentApplies) {
         equipmentApplyRepository.saveAll(equipmentApplies);
    }

    @Override
    public Page<EquipmentApply> listEquipmentApplyByNameLikeAndStateLike(String equipmentName, String state, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        state="%"+state+"%";
        return equipmentApplyRepository.findByEquipmentInfo_EquipmentNameLikeAndEquipmentApplyStateLike(equipmentName,state,pageable);
    }

    @Override
    public void audit(Long equipmentApplyId, String state, TeacherInfo teacher) throws Exception {
        EquipmentApply equipmentApply=equipmentApplyRepository.getOne(equipmentApplyId);
        if(equipmentApply==null){
            throw  new Exception("此器材申请记录不存在！");
        }
        if(!"0".equals(equipmentApply.getEquipmentApplyState())){
            throw new Exception("此器材申请信息已被审核！");
        }
        Integer count=equipmentApply.getEquipmentApplyCount();
        if("1".equals(state)){
            EquipmentInfo equipmentInfo=equipmentApply.getEquipmentInfo();
            EquipmentRepertory equipmentRepertory=equipmentRepertoryService.findByEquipment(equipmentInfo);
            if(equipmentRepertory==null||equipmentRepertory.getEquipmentCount()<count){
                throw new Exception("此器材库存没有或小于申请数量,无法通过！");
            }
        }
        equipmentApply.setEquipmentApplyAudit(teacher);
        equipmentApply.setEquipmentApplyState(state);
        this.update(equipmentApply);
    }
}
